const Record = require('../mongodb/record');
const reply = require('../utils/reply');
const app = require('../app');

const RecordController = {
    record: async (ctx, next) => {

        let data = ctx.request.body;

        if (!data.operator_code) {
            await reply(ctx, 'null operator_code');
            return;
        }

        if (!data.task_id) {
            await reply(ctx, 'null task_id');
            return;
        }

        const record = new Record({
            ...data,
            is_sub: 0,
            error_log: '',
            run_log: ''
        });

        await record.save().then(doc => {
            reply(ctx, null, (ctx, result) => {
                result.data = doc._id;
                ctx.json(result);
            });
        }).catch(err => {
            reply(ctx, 'save data error');
        });

    }
};

module.exports = RecordController;
